package com.huang.educenter.controller;


import com.huang.educenter.entity.UcenterMember;
import com.huang.educenter.entity.vo.LoginVo;
import com.huang.educenter.entity.vo.RegisterVo;
import com.huang.educenter.service.UcenterMemberService;
import com.hunag.utils.JwtUtils;
import com.hunag.utils.R;
import com.hunag.utils.orderVo.UcenterOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-29
 */
@RestController
@RequestMapping("/educenter/ucenter-member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService service;



    @PostMapping("login")
    public R loginUser(@RequestBody LoginVo loginVo){
        //返回Token值
        String token=service.login(loginVo);

        return R.ok().data("token",token);
    }


    @PostMapping("register")
    public R registUser(@RequestBody RegisterVo  registerVo){
        service.registerUser(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getInfo(HttpServletRequest request){
        String id = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member= service.getById(id);
        return R.ok().data("member",member);

    }

    //根据id获取信息
    @GetMapping("getUcenter/{id}")
    public UcenterOrder getUcenter(@PathVariable String id){
        UcenterMember member = service.getById(id);
        UcenterOrder ucenterOrder = new UcenterOrder();
        BeanUtils.copyProperties(member,ucenterOrder);
        return ucenterOrder;
    }

    //查询某一天的注册人数
    @PostMapping("getRegisterNum/{day}")
    public R getRegisterNum(@PathVariable String day){
       Integer count= service.countRegisterDay(day);
        return R.ok().data("count",count);
    }
}

